<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>WS客户端 - 2号用户Rose</h1>
<button type="button" onclick="socket.close()">下线</button>
<button type="button" onclick="socket.send('大家好，我是Rose')">群发 "大家好，我是Rose"</button>
<section id="screen"></section>
<script>
    let socket;
    onload = () => {
        let screen = document.querySelector("#screen");
        if (!WebSocket) {
            screen.innerText = "当前浏览器不支持WebSocket!";
            return;
        }
        // 使用ws://协议连接S端，并传递Rose的用户ID过去
        socket = new WebSocket("ws://localhost:5001/danmu/2");
        socket.onopen = () => screen.innerText = "2号用户Rose上线了";
        socket.onclose = () => screen.innerText = "2号用户Rose下线了";
        socket.onerror = () => screen.innerText = "2号用户Rose连接或通信异常";
        socket.onmessage = resp => screen.innerText = resp['data'];
    };
</script>
</body>
</html>